<?php

class DBUtil {
	
	static function bindInsert( array $hashMap ) {
		$count = count($hashMap);
		$markers = implode(',', array_pad( array(), $count, '?') );
		$bind = array_values($hashMap);
		$fields = array_keys($hashMap);
		$sqlFields = implode( ',', $fields);
		return array( $sqlFields, $markers, $bind );
		// list( $fields, $markers $bind,  )
	}

	/**
	 *
	 * @param array $setMapping
	 * @param array $whereMapping
	 * @return array ($setSQL, $whereSQL, $bindParameters)
	 */
	static function bindUpdate( array $setMapping, array $whereMapping ) {
		$fields = array_keys($setMapping);
		array_walk( $fields, array(self, 'appendPlaceholder') );
		$setSQL = implode(', ', $fields);
		
		$fields = array_keys($whereMapping);
		array_walk( $fields, array(self, 'appendPlaceholder') );
		$whereSQL = implode(' AND ', $fields);
		return array( $setSQL, $whereSQL, array_merge( array_values($setMapping), array_values($whereMapping) ) );
		// list( $fields, $crit, $bind )
	}

	/**
	 * @param array $hashMap
	 * @return array ($where, $bind)
	 */
	static function bind( array $hashMap ) {
		$vals = array_values($hashMap);
		$fields = array_keys($hashMap);
		array_walk( $fields, array(self, 'appendPlaceholder') );
		$sqlMapping = implode(' AND ', $fields);
		return array( $sqlMapping, $vals );
		// list( $crit, $bind )
	}
	
	private static function appendPlaceholder(&$val, $key) {
		$val = "$val = ?";
	}
	
}